<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>throttle</title>
  </head>
  <body>
    <div id="container"></div>
  </body>
  <script>
    var count = 1;
    var container = document.getElementById("container");

    function getUserAction(e) {
      //   console.log(e);
      container.innerHTML = count++;
    }

    // function throttle(fn,wait){
    //     let timeout = null
    //     return function(){
    //         let that = this
    //         let arg = arguments
    //         if(!timeout){
    //             timeout = setTimeout(() => {
    //                 timeout = null
    //                 fn.apply(that,arg)
    //             }, wait);
    //         }
    //     }
    // }
    function throttle(fn,wait){
        let timeout = null
        return function(){
            let that = this
            let arg = arguments
            if(!timeout){
                fn.apply(that,arg)
                timeout = setTimeout(() => {
                    timeout = null    
                }, wait);
            }
        }
    }
    container.onmousemove = throttle(getUserAction, 1000);
  </script>
  <style>
    #container {
      width: 100%;
      height: 200px;
      line-height: 200px;
      text-align: center;
      color: #fff;
      background-color: #444;
      font-size: 30px;
    }
  </style>
</html>
